﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using cwtrock.service.impl;
using cwtrock.model;
using cwtrock.util;
using cwtrock.syscommon;

namespace cwtrock.view
{
    public partial class Addaccount : Form
    {
        private AccountsServiceImpl accountService = new AccountsServiceImpl();
        private string acctType = "acct_type";
        private string acctParentid ;
        private string acctBorL="";
        private Accounts curAccount;
        private bool isUpdate = false;

        public Addaccount(string acctParentid,Accounts account)
        {
            InitializeComponent();
            initCombox();
            this.acctParentid = acctParentid;
            this.curAccount = account;
            errorAcctcode.Clear();
            errorAcctname.Clear();

            if (this.curAccount!=null &&( this.curAccount.Acctcode != null && !this.curAccount.Acctcode.Equals("")))
            {
                isUpdate = true;
                this.initForm();
            }
            this.acct_type_combo.SelectedItem = this.curAccount.Accttype;
        }

        private void initForm()
        {
            this.curAccount = accountService.getAcctsByCode(this.curAccount.Acctcode);
            this.acct_code_txt.Text = this.curAccount.Acctcode;
            this.acct_name_txt.Text = this.curAccount.Acctname;
            if (this.curAccount.Acctbalancetype.Equals(SysConst.BORROW))
                this.acct_borrow_radio.Checked = true;
            else
                this.acct_loan_radio.Checked = true;

        }

        public void initCombox() {

            List<SysData> acctTypels = accountService.getSysDataByType(acctType);
            acct_type_combo.DataSource = acctTypels;
            acct_type_combo.DisplayMember = "itemname";
            acct_type_combo.ValueMember = "itemcode";
        
        }


        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            bool b1 = SimpleTool.validaing(errorAcctcode,acct_code_txt,10,"会计科目");
            bool b2 = SimpleTool.validaing(errorAcctname, acct_name_txt, 20, "会计名称");

            if (!(b1 && b2))
                return ;

            if(acct_borrow_radio.Checked)
                acctBorL = SysConst.BORROW;
            else if(acct_loan_radio.Checked)
                 acctBorL = SysConst.LOAN;


            curAccount.Abid = 0;
            curAccount.Acctcode = this.acct_code_txt.Text.Trim();
            curAccount.Acctname = this.acct_name_txt.Text.Trim();
            curAccount.Parentid = this.acctParentid;
            
            SysData seldata = (SysData)(this.acct_type_combo.SelectedItem);
            curAccount.Accttype = seldata.Itemcode;
            curAccount.Acctbalancetype = this.acctBorL;

            int rtnrows = 0;
            if(isUpdate)
                rtnrows = accountService.update(curAccount) ;
            else
                rtnrows =  accountService.save(curAccount);

            if (rtnrows==0) {

                throw new Exception("科目保存失败！");
            }
         
            this.Close();
            
        }


    public    Accounts CurAccount
        {
            get { return curAccount; }
            set { curAccount = value; }
        }

      
    }
}
